﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceInterfaces.Headquarters;
using DTO.Common;
using System.ServiceModel;
using DAO.Headquarter;
using System.Transactions;
using DTO.Headquarters;
using ServiceImplementation.Common;

namespace Headquarter
{
    [ServiceBehavior(
        ReleaseServiceInstanceOnTransactionComplete= true,
        TransactionAutoCompleteOnSessionClose = true,
        TransactionIsolationLevel = IsolationLevel.ReadCommitted,
        IncludeExceptionDetailInFaults = true)]
    class Headquarter : IHeadquartersManagement, IHeadquartersSales
    {
        #region management

        public void AddProduct(Product product)
        {
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("AddProduct");
            Console.WriteLine(product.ToString());

            ProductDAO productDAO = new ProductDAO(ConfigSettings.provider, ConfigSettings.connectionString);
            productDAO.AddProduct(product);
        }

        public void AddSupplier(Supplier supplier)
        {
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("AddSupplier");
            Console.WriteLine(supplier.ToString());

            SupplierDAO supplierDAO = new SupplierDAO(ConfigSettings.provider, ConfigSettings.connectionString);
            supplierDAO.AddSupplier(supplier);
        }

        public List<Supplier> GetSuppliers()
        {
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("GetSuppliers");

            SupplierDAO supplierDAO = new SupplierDAO(ConfigSettings.provider, ConfigSettings.connectionString);
            return supplierDAO.GetSuppliers();
        }

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public ProductList GetProductsToOrder(int page, int recordsPerPage, OrderStatus orderStatus)
        {
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("GetProductsToOrder");
            Console.WriteLine(string.Format("\tpage = {0} recordsPerPage = {1} orderStatus = {2}", page, recordsPerPage, orderStatus));

            OrderDAO orderDAO = new OrderDAO(ConfigSettings.provider, ConfigSettings.connectionString);
            return new ProductList { 
                 Products = orderDAO.GetProductsToOrder(page, recordsPerPage, orderStatus),
                 Total = orderDAO.GetNumberOfProductsToOrder(orderStatus)
            };
        }

        public void ProductOrdered(int productCode)
        {
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("ProductOrdered");
            Console.WriteLine(string.Format("\tproductCode = {0}", productCode));

            OrderDAO orderDAO = new OrderDAO(ConfigSettings.provider, ConfigSettings.connectionString);
            orderDAO.MarkProductAsOrdered(productCode);
        }

        public void UpdateStock(int productCode, int amount)
        {
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("UpdateStock");
            Console.WriteLine(string.Format("\tproductCode = {0} amount = {1}", productCode, amount));

            // Does not needs a transaction.
            // It's a single operation
            OrderDAO productDAO = new OrderDAO(ConfigSettings.provider, ConfigSettings.connectionString);
            productDAO.UpdateProductStock(productCode, amount);
        }

        #endregion

        #region sales

        [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
        public void SellProduct(int productCode, int amount)
        {
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("SellProduct");
            Console.WriteLine(string.Format("\tproductCode = {0} amount = {1}", productCode, amount));

            //ProductDAO productDAO = new ProductDAO(ConfigSettings.provider, ConfigSettings.connectionString);
            //foreach(SoldProduct product in products)
            //{
            //    productDAO.ProductSold(product.Product.Code);
            //}

            ProductDAO productDAO = new ProductDAO(ConfigSettings.provider, ConfigSettings.connectionString);
            productDAO.ProductSold(productCode, amount);
        }

        #endregion
    }
}
